package com.lgx.mall.basic.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.lgx.common.basic.dao.GeneralDao;
import com.lgx.common.exception.DataException;
import com.lgx.mall.basic.dao.IAddressDao;
import com.lgx.mall.basic.entity.Address;

/**
 * 
 * @author ZHANGCHAO
 *
 */

@Component("AddressDao")
public class AddressDao extends GeneralDao implements IAddressDao{

	protected Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * 创建地址
	 */
	@Override
	public Address createAddress(Address address) throws DataException {
		this.getHibernateTemplate().save(address);
		log.info("[创建address]:"+address.getId());
		return address;
	}

	/**
	 * 修改
	 */
	@Override
	public Address updateAddress(Address address) throws DataException {
		this.getHibernateTemplate().update(address);
		log.info("[修改address]:"+address.getId());
		return address;
	}

	/**
	 * 删除
	 */
	@Override
	public void deleteAddress(Address address) throws DataException {
		log.info("[删除news]:"+address.getId());
		this.getHibernateTemplate().delete(address);
	}

	/**
	 * 
	 */
	@Override
	public void deleteAddressById(long id) throws DataException {
		log.info("[删除news]:"+id);
		this.deleteAddress(this.findAddressById(id));
	}

	@Override
	public void deleteAddress(String[] ids) throws DataException {
		for (String id:ids) {
			this.deleteAddressById(Long.parseLong(id));
		}
	}

	/**
	 * 根据id查找address
	 */
	@Override
	public Address findAddressById(long id) {
		return this.getHibernateTemplate().get(Address.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Address> findAddressList(long userId) {
		String sql = "from Address a where a.user.id = " + userId;
		sql += " order by a.id asc";
		return this.getHibernateTemplate().find(sql);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Address> findAddressList(long userId, Integer isdefault) {
		String sql = "from Address a where a.user.id = " + userId+" and a.isdefault = "+isdefault;
		sql += " order by a.id asc";
		return this.getHibernateTemplate().find(sql);
	}


}
